class Session {
    constructor(key, value) {
        this.key = key;
        this.value = value;
        this.expire = -1;
    }
}

class SessionStorage {
    constructor(){
        this.sessions = [];
    }

    add(key, value){
        const session = new Session(key, value);
        this.replace(session);
    }

    get(key) {
        return this.sessions.find(s => s.key === key)
    }

    replace(session) {
        const existIndex = this.findIndex(session.key);

        if(existIndex >= 0) {
            this.sessions[existIndex].value = session.value;
        } else {
            this.sessions.push(session);
        }
    }

    exist(key) {
        if(!key) return false;
        return this.sessions.findIndex(s => s.key === key) >= 0;
    }

    findIndex(key) {
        return this.sessions.findIndex(s => s.key === key);
    }

    remove(key){
        const existIndex = this.findIndex(key);

        if(existIndex >= 0) {
            this.sessions.splice(existIndex, 1);
        }
    }
}

const sessionStorage = new SessionStorage();

module.exports = sessionStorage;